package com.my.study.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * 自定义初始化管道
 *
 * @author: yidujun
 * @create: 2021/04/12 15:27
 */
public class ServerInitializer extends ChannelInitializer<SocketChannel> {

    /**
     * 初始化通道
     *
     * {@link HttpServerCodec}是netty提供的处理http的 编-解码器
     *
     * @param ch
     * @throws Exception
     */
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        //=========向管道加入处理器=========
        // 得到管道
        ChannelPipeline pipeline = ch.pipeline();
        // 1.新增http编-解码处理器
        pipeline.addLast("MyHttpServerCodec", new HttpServerCodec());
        // 2.新增自定义http处理器
        pipeline.addLast("MyHttpServerHandler", new HttpServerHandler());

        System.out.println("OK~~~~~");
    }
}
